﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RewardGenerator : MonoBehaviour {

	public List<Reward> prefabs;
	private float lineOne;
	private float lineTwo;
	public List<RewardGroup> gList;
	public float baseX;

	void Start () {
		gList = new List<RewardGroup> ();
		baseX = 0;
		InitGenerate ();
	}
	
	void Update () {
		float x = gList [0].transform.position.x + gList [0].getTotalWidth ();
		if (Camera.main.GetComponent<CameraMoved> ().OutLeft (x)) {
			var group = gList [0];
			gList.Remove (group);
			foreach (Transform child in group.transform) {
				GameObject.Destroy (child.gameObject);
			}
			GameObject.Destroy (group.gameObject);
			group = GenerateGroup ();
			gList.Add (group);
		}
	}

	public RewardGroup GenerateGroup(){
		GameObject obj = new GameObject ("group");
		var group = obj.AddComponent<RewardGroup> ();
		obj.transform.parent = transform;
		group.rewardList = generateGroup ();
		group.transform.position = new Vector3(baseX, 7, 0);
		for (var k = 0; k < group.rewardList.Count; k++) {
			var childPos = group.rewardList [k].transform.position;
			group.rewardList [k].transform.parent = group.transform;
			group.rewardList [k].transform.position = new Vector3 (group.transform.position.x + childPos.x,
				group.transform.position.y + childPos.y, group.transform.position.z+childPos.z);
			group.rewardList [k].baseY = group.rewardList [k].transform.position.y;
		}
		int rewardInterval = Random.Range (7, 11);
		baseX = baseX + group.getTotalWidth() + rewardInterval * group.rewardWidth();
		return group;
	}

	public void InitGenerate(){
		int count = Random.Range(7, 10);
		baseX = Camera.main.ScreenToWorldPoint (new Vector2 (Screen.width, Screen.height)).x;
		for (var i = 0; i < count; i++) {
			var group = GenerateGroup ();
			gList.Add (group);
		}
	}

	public List<Reward> generateGroup(){
		int one = Random.Range (1, 5);
		int two = Random.Range (1, 5);
		List<Reward> list = new List<Reward>();
		for (var i = 0; i < one; i++) {
			int index = Random.Range (0, prefabs.Count);
			var reward = GameObject.Instantiate (prefabs [index]);
			reward.Init ();
			reward.GetComponent<SpriteRenderer> ().sortingOrder = 2;
			if (i == 0) {
				reward.transform.position = new Vector3(0, 1f, 0);
			} else {
				bool addVoid = Random.value < 0.2f ? true : false;
				if (addVoid) {
					reward.transform.position = new Vector3(list [list.Count - 1].transform.position.x + (reward.width*1.25f)*2, 1f, 0);
				} else {
					reward.transform.position = new Vector3(list [list.Count - 1].transform.position.x + (reward.width*1.25f), 1f, 0);
				}
			}
			list.Add(reward);
		}
		for (var i = 0; i < two; i++) {
			int index = Random.Range (0, prefabs.Count);
			var reward = GameObject.Instantiate (prefabs [index]);
			reward.Init ();
			reward.GetComponent<SpriteRenderer> ().sortingOrder = 2;
			if (i == 0) {
				reward.transform.position = new Vector3(0, 0, 0);
			} else {
				bool addVoid = Random.value < 0.2f ? true : false;
				if (addVoid) {
					reward.transform.position = new Vector3(list [list.Count - 1].transform.position.x + (reward.width*1.25f)*2, 0, 0);
				} else {
					reward.transform.position = new Vector3(list [list.Count - 1].transform.position.x + (reward.width*1.25f), 0, 0);
				}
			}
			list.Add(reward);
		}
		return list;
	}

}
